<?php
namespace app\api\controller;
use think\Db;
use wxBizDataCrypt\WxBizDataCrypt;
use SmsMsg\SmsMsg;

class Phone extends Common
{
    private $appId = 'wx6bba745d6119053c';
    private $appSecret = '796fc0a1c07399769f385a1bfd1e1d1c';

    public function index()
    {
        $params =  $this->params;
        $url = 'https://api.weixin.qq.com/sns/jscode2session';
        $data = [
            'appid'=>$this->appId,
            'secret'=>$this->appSecret,
            'js_code'=> $params['code'],
            'grant_type'=>'authorization_code'
        ];
        $res =  http_get($url,$data);
        $res = decode($res);
        if(isset($res['session_key'])&&isset($res['openid'])){
            $session_key = $res['session_key'];
            $openid = $res['openid'];
            $pc = new WxBizDataCrypt($this->appId,$session_key);
            $errCode = $pc->decryptData($params['encryptedData'], $params['iv'],$data);
            if ($errCode == 0) {
                $data = decode($data);
                $phone = $data['phoneNumber'];
                $this->table = 'code';
                $is_exit = obj_decode( $this->select(['phone'=>$phone]) );
                if($is_exit['code']){
                    return_msg(2,'该手机号已授权！',
                    ['phone'=>$is_exit['data'][0]['phone'],'open_id'=>$is_exit['data'][0]['open_id']]);
                }
                else{
                    $data =  [
                        'phone'=>$phone,
                        'open_id'=>$openid
                    ];
                    $res = obj_decode( $this->create($data) );
                    if($res['code']){
                        return_msg(1,'手机绑定成功！',$data);
                    }
                    else{
                        return_msg(0,'手机绑定失败！',[]);
                    }
                }
            } else {
                return_msg(0,'手机绑定失败！',[]);
            }
        }
        else{
            return_msg(0,'手机绑定失败！',[]);
        }

    }

    public function poster1(){
        $this->table = 'txt';
        $res = obj_decode( $this->read(1) );
        if($res['code']){
            return_msg(1,'获取成功！',$res['data']['poster1']);
        }
        else{
            return_msg(0,'获取失败');
        }
    }

    public function poster2(){
        $this->table = 'txt';
        $res = obj_decode( $this->read(1) );
        if($res['code']){
            return_msg(1,'获取成功！',$res['data']['poster2']);
        }
        else{
            return_msg(0,'获取失败');
        }
    }

    public function active(){
        $this->table = 'txt';
        $wenan = obj_decode( $this->read(1) );
        $this->table = 'activation_code';
        $params = $this->params;
        $res = obj_decode( $this->select( ['activation_code'=>$params['activationCode']]) );
        
        if($res['code']){
            if($res['data'][0]['is_used']){
                return_msg(0,$wenan['data']['fail_txt']);
            }
            else{
                $activation_code = $res['data'][0]['activation_code'];
                $data = [
                    'activation_code'=>$activation_code,
                    'activation_time'=>date('Y-m-d H:i:s')
                ];
                $res0 = Db::name('code')->where(['phone'=>$params['phone']])->find();

                if($res0['activation_code']){
                    return_msg(0,'该手机号已激活过激活码！');
                }
                else{
                    $res1 = Db::name('code')
                        ->where(['phone'=>$params['phone']])
                        ->update($data);
                    if(!$res1){
                        return_msg(-1,'尚未授权,请先授权！');
                    }
                    $res2 = Db::name('activation_code')
                        ->where(['activation_code'=>$activation_code])
                        ->update(['is_used'=>1]);
                    if($res1&&$res2){
                        $this->smsMsg($params['phone']);
                        return_msg(1,$wenan['data']['success_txt'],$data);
                    }
                    else{
                        return_msg(0,'激活失败！');
                    }
                }
            }
        }
        else{
            return_msg(0,$wenan['data']['fail1_txt']);
        }
    }

    public function banner(){
       $res =  Db::name('banner')
       ->where('is_delete',0)->select();
       if($res){
           return_msg(1,'请求成功！',$res);
       }
       else{
           return_msg(1,'请求失败！');
       }
    }

    public function tel(){
        $this->table = 'txt';
        $res = obj_decode( $this->read(1) );
        if($res['code']){
            $data =  [
                'phone'=>$res['data']['phone']
            ];
            return_msg(1,'获取成功！',$data);
        }
        else{
            return_msg(0,'获取失败！');
        }
    }

    private function smsMsg($phone=''){
        $msg = new SmsMsg('LTAITNUIRtBNypeQ','zEQK7Pg3r0LqscnSK3mhTcZtRKtZ56');
        $response = $msg->sendSms(
            "CPF",              // 短信签名
            "SMS_151577483",          // 短信模板编号
            $phone
        );
        $result = (array)$response;
        if($result['Code']=='OK'){
            $data=  ['code'=>1,'msg'=>'发送成功！'];
        }
        else{
            $data = ['code'=>0,'msg'=>'发送失败！'];
        }
        return $data;
    }


}